<?php # CustomerReservationsSearch.php
session_start();
require "hotels.class.php";
require "customers.class.php";
require "roombookings.class.php";
require "bookings.class.php";
require "payments.class.php";
require "rooms.class.php";
require "roomtypes.class.php";

/*
 * Specify the field names that are in the form.
 */
$allowedFields = array(
 			'customerId',
			'user_name'
);

/*
 * Specify the field names that are required...
 */
$requiredFields = array(
			'customerId'
);

$error = false;
$message = array();
foreach($_SESSION AS $key => $value)
{
	// first make sure this is an allowed field
	if(in_array($key, $allowedFields))
	{
		$$key = $value;
		
		// is this a required field?
		if(in_array($key, $requiredFields) && $value == '')
		{
			$error = true;
			$message['error'] = 'The field '.$key.' is required.';
		}
	}
	else
	{
		$error = true;
		$message['error'] = 'The field '.$key.' does not exist.';
	}
}

if (!$error) {
	$customerId = '';
	if (isset($_SESSION['customerId'])){
		$customerId = $_SESSION['customerId'];
	}
	else {
		$customerId = '';
	}
	
	//*** Delete Condition ***//
	if(isset($_GET['Action']) && isset($_GET['booking_id'])) {
		if($_GET["Action"] == "Delete") {
			$booking_id = $_GET['booking_id'];
			$booking = Bookings::loadUnique($booking_id);
			if($booking->getCustomerId() == $customerId) {
				Roombookings::deleteRecords("`bookingId`='$booking_id'");
				Payments::deleteRecords("`bookingId`='$booking_id'");
				Bookings::deleteRecords("`bookingId`='$booking_id'");
			}
		}
	}
	
	$bookings = Bookings::loadByFKCustomerid($customerId);
	$bookings_array = array();
	$return_arr = array();
	
	$count = count($bookings);
	$total = 0;
	for ($i = 0; $i < $count; $i++) {
		$bookingId = $bookings[$i]->getBookingId();
		$customerId = $bookings[$i]->getCustomerId();
		$dateBookingMade = $bookings[$i]->getDateBookingMade();
		$dateFrom = $bookings[$i]->getDateFrom();
		$dateTo = $bookings[$i]->getDateTo();
		$paymentDueDate = $bookings[$i]->getPaymentDueDate();
		$paymentDueAmount = $bookings[$i]->getPaymentDueAmount();
		$paymentMadeOn = $bookings[$i]->getPaymentMadeOn();
		$comments = $bookings[$i]->getComments();
		
		// get the room bookings info
		$roomBookingss = $bookings[$i]->getRoombookingss();
		$roomBookings_array = array();
		$rooms_array = array();
		$numRoomBookings = count($roomBookingss);
		for ($j = 0; $j < $numRoomBookings; $j++) {
			$numGuests = $roomBookingss[$j]->getNumGuests();
			
			// get the hotel information
			$hotel = $roomBookingss[$j]->getHotels();
			$hotelId = $hotel->getHotelId();
			$hotelName = $hotel->getHotelName();
			$hotelURL = $hotel->getHotelURL();
			
			// get the room information
			$room = $roomBookingss[$j]->getRooms();
			$roomId = $room->getRoomId();
			$roomFloor = $room->getRoomFloor();
			$roomType = $room->getRoomtypes();
			$roomTypeCode = $roomType->getRoomTypeCode();
			$roomStandardRate = $roomType->getRoomStandardRate();
			$roomTypeDescription = $roomType->getRoomTypeDescription();
			$roomSmokingYN = $roomType->getSmokingYN();
			$roomBookings_array['num_guests'] = $numGuests;
			$roomBookings_array['hotel_id'] = $hotelId;
			$roomBookings_array['hotel_name'] = $hotelName;
			$roomBookings_array['hotel_URL'] = $hotelURL;
			$roomBookings_array['room_id'] = $roomId;
			$roomBookings_array['room_floor'] = $roomFloor;
			$roomBookings_array['room_typecode'] = $roomTypeCode;
			$roomBookings_array['room_standardrate'] = $roomStandardRate;
			$roomBookings_array['room_typedescription'] = $roomTypeDescription;
			$roomBookings_array['room_smokingyn'] = $roomSmokingYN;
			array_push($rooms_array, $roomBookings_array);
		}
		
		$bookingId = $bookings[$i]->getBookingId();
		$customerId = $bookings[$i]->getCustomerId();
		$dateBookingMade = $bookings[$i]->getDateBookingMade();
		$dateFrom = $bookings[$i]->getDateFrom();
		$dateTo = $bookings[$i]->getDateTo();
		$paymentDueDate = $bookings[$i]->getPaymentDueDate();
		$paymentDueAmount = $bookings[$i]->getPaymentDueAmount();
		$paymentMadeOn = $bookings[$i]->getPaymentMadeOn();
		$comments = $bookings[$i]->getComments();
		
		$bookings_array['booking_id'] = $bookingId;
		$bookings_array['customer_id'] = $customerId;
		$bookings_array['date_booking_made'] = $dateBookingMade;
		$bookings_array['date_from'] = $dateFrom;
		$bookings_array['date_to'] = $dateTo;
		$bookings_array['payment_due_date'] = $paymentDueDate;
		$bookings_array['payment_due_amount'] = $paymentDueAmount;
		$bookings_array['payment_made_on'] = $paymentMadeOn;
		$bookings_array['comments'] = $comments;
		$bookings_array['rooms_info'] = $rooms_array;
		array_push($return_arr, $bookings_array);
		$total = $total + 1;
	}
	$numBookings = array();
	if($total > 0) {
		$numBookings['count'] = $total;
		array_push($return_arr, $numBookings);
	}
	else {
		$numBookings['count'] = 0;
		array_push($return_arr, $numBookings);
	}
	
	print json_encode($return_arr);
}
else {
	$error = true;
	print json_encode($message);
}
?>
